<?php
namespace App\Repositories\Main;

use App\Models\Main\MsgDetailModel;
use App\Models\Main\MsgTalkModel;
use App\Repositories\BaseRepository;

class MsgTalkRepository extends BaseRepository
{
    protected $repository;
    protected $detailRepository;

    public function __construct()
    {
        $this->repository = new MsgTalkModel();
        $this->detailRepository = new MsgDetailModel();
    }

    public function getIndexList(){
        return $this->repository
            ->orderBy('id','asc')
            ->get();
    }

    /**
     * 主页新消息提醒
     */
    public function getNewTips($uid,$genre)
    {
        return $this->repository
            ->where([
                ['genre','=',$genre],
                ['own','=',$uid],
                ['own_new','>',0],
            ])
            ->orWhere([
                ['genre','=',$genre],
                ['other','=',$uid],
                ['other_new','>',0],
            ])
            ->count();
    }

    /**
     * 会话列表
     */
    public function getTalks($uid,$genre)
    {
        return $this->repository
            ->where([
                ['genre','=',$genre],
                ['own','=',$uid],
            ])
            ->orWhere([
                ['genre','=',$genre],
                ['other','=',$uid],
            ])->get();
    }

    /**
     * 会话列表
     */
    public function getDetails($uid,$genre)
    {
        return $this->repository
            ->where([
                ['genre','=',$genre],
                ['own','=',$uid],
            ])
            ->orWhere([
                ['genre','=',$genre],
                ['other','=',$uid],
            ])->get();
    }

    /**
     * 新建会话
     */
    public function setAdd($data)
    {
        $data['created_at'] = time();
        $data['updated_at'] = time();
        return $this->repository->insertGetId($data);
    }

    /**
     * 更新会话记录
     */
    public function setModify($talk,$from)
    {
        if ($from==1) {
            $dataArr = array(
                'other_new' => $talk->other_new + 1,
                'updated_at' => time(),
            );
        } else {
            $dataArr = array(
                'own_new' => $talk->own_new + 1,
                'updated_at' => time(),
            );
        }
        $this->repository->where('id',$talk->id)
            ->update($dataArr);
    }

    /**
     * 新消息清0
     */
    public function setLast($talk,$from)
    {
        if ($from==1) {
            $dataArr = array(
                'other_new' => 0,
                'updated_at' => time(),
            );
        } else {
            $dataArr = array(
                'own_new' => 0,
                'updated_at' => time(),
            );
        }
        $this->repository->where('id',$talk->id)
            ->update($dataArr);
    }

    public function getShowOne($id)
    {
        return $this->repository->where('id',$id)->first();
    }

    /**
     * 获取会话
     */
    public function getAppJobShow($uid,$uid2)
    {
        return $this->repository
            ->where([
                ['own',$uid],
                ['other',$uid2],
                ['genre',2],
            ])
            ->orWhere([
                ['own',$uid2],
                ['other',$uid],
                ['genre',2],
            ])
            ->first();
    }
}